package com.xrh.xdemo.service.service;


import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.entity.RoleVsMenuDTO;
import com.xrh.xdemo.api.request.addRequest.RoleVsMenuAddRequest;
import com.xrh.xdemo.api.request.queryRequest.RoleVsMenuQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.RoleVsMenuUpdateRequest;
import com.xrh.xdemo.service.entity.RoleVsMenuEntity;
import com.xrh.xdemo.service.mapper.RoleVsMenuMapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;

@Service
public class RoleVsMenuService {

	@Resource
	RoleVsMenuMapper roleVsMenuMapper;
	
	/**
	 * @function 获取角色id关联的菜单id集合
	 * @param roleId
	 * @author 肖荣辉
	 * @date 2023年12月8日
	*/
	public Set<Long> getMenuIdsByRoleId(Long roleId){
		return getMenuIdsByRoleIds(CollUtil.newArrayList(roleId));
	}
	
	/**
	 * @function 获取角色ids关联的菜单id集合
	 * @param roleIds
	 * @author 肖荣辉
	 * @date 2023年12月8日
	*/
	public Set<Long> getMenuIdsByRoleIds(Collection<Long> roleIds){
		
		if(CollUtil.isEmpty(roleIds))
			return new HashSet<Long>();
		
		LambdaQueryWrapper<RoleVsMenuEntity> queryWrapper = new LambdaQueryWrapper<RoleVsMenuEntity>();
		
		queryWrapper.in(RoleVsMenuEntity::getSysRoleId, roleIds);
		
		return CollStreamUtil.toSet(roleVsMenuMapper.selectList(queryWrapper), RoleVsMenuEntity::getMenuId);
		
	}
	
	/**
	 * @function 新增RoleVsMenu
	 * @param roleVsMenuAddRequest
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public Long addRoleVsMenu(RoleVsMenuAddRequest roleVsMenuAddRequest) {
		
		RoleVsMenuEntity roleVsMenuEntity = new RoleVsMenuEntity();
		
		BeanUtil.copyProperties(roleVsMenuAddRequest , roleVsMenuEntity , CopyOptions.create().ignoreNullValue());
		
		roleVsMenuMapper.insert(roleVsMenuEntity);
		
		return roleVsMenuEntity.getId();
	}
	
	/**
	 * @function  根据菜单id删除关联关系
	 * @param menuId
	 * @author 肖荣辉
	 * @date 2023年12月14日
	*/
	public void deleteRoleVsMenuByMenuId(Collection<Long> menuIds) {
		
		LambdaQueryWrapper<RoleVsMenuEntity> queryWrapper = new LambdaQueryWrapper<RoleVsMenuEntity>();
		
		queryWrapper.in(RoleVsMenuEntity::getMenuId, menuIds);
		
		roleVsMenuMapper.delete(queryWrapper);;
		
	}
	
	/**
	 * @function 删除RoleVsMenu
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public void deleteRoleVsMenuById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		RoleVsMenuEntity roleVsMenuEntity = roleVsMenuMapper.selectById(id);
		
		if(roleVsMenuEntity == null)
			throw new BusinessException("id不合法");
		
		UpdateWrapper<RoleVsMenuEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", id);
		
		//因为注解了@TableLogic , 自动会把删除标志位设置为1 
		roleVsMenuMapper.delete(updateWrapper);
		
	}

	/**
	 * @function 修改RoleVsMenu
	 * @param roleVsMenuUpdateRequest
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public void updateRoleVsMenu(RoleVsMenuUpdateRequest roleVsMenuUpdateRequest) {
		
		if(roleVsMenuUpdateRequest.getId() == null)
			throw new BusinessException("id不能为空");
		
		RoleVsMenuEntity roleVsMenuEntity = roleVsMenuMapper.selectById(roleVsMenuUpdateRequest.getId());
		
		if(roleVsMenuEntity == null)
			throw new BusinessException("id不合法");
		
		BeanUtil.copyProperties(roleVsMenuUpdateRequest , roleVsMenuEntity , CopyOptions.create().ignoreNullValue());
		
		roleVsMenuMapper.updateById(roleVsMenuEntity);
		
	}

	/**
	 * @function 根据ID查询RoleVsMenu Entity
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public RoleVsMenuEntity findRoleVsMenuEntityById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		RoleVsMenuEntity roleVsMenuEntity = roleVsMenuMapper.selectById(id);
		
		return roleVsMenuEntity;
	}
	
	/**
	 * @function 根据ID查询RoleVsMenu DTO
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public RoleVsMenuDTO findRoleVsMenuById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
	
		RoleVsMenuEntity roleVsMenuEntity = roleVsMenuMapper.selectById(id);
		
		RoleVsMenuDTO  roleVsMenuDTO = BeanUtil.toBean(roleVsMenuEntity , RoleVsMenuDTO.class);
		
		return roleVsMenuDTO;
	}

	/**
	 * @function 根据RoleVsMenuQueryVo查询RoleVsMenu列表
	 * @param roleVsMenuQueryRequest
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public CommonResult<CommonPage<RoleVsMenuDTO>> findRoleVsMenuList(RoleVsMenuQueryRequest roleVsMenuQueryRequest) {
		
		Page<RoleVsMenuEntity> page = roleVsMenuQueryRequest.createPage();

		QueryWrapper<RoleVsMenuEntity> queryWrapper = roleVsMenuQueryRequest.createQuery();
		
		roleVsMenuMapper.selectPage(page, queryWrapper);
		
		List<RoleVsMenuDTO> dtoList = BeanUtil.copyToList(page.getRecords() , RoleVsMenuDTO.class);
		
		return CommonResult.success(CommonPage.create(roleVsMenuQueryRequest.getPageNum() , roleVsMenuQueryRequest.getPageSize() , dtoList, page.getTotal()));
	}

	/**
	 * @function 重置角色和菜单的绑定关系信息
	 * @author 肖荣辉
	 * @date 2023年12月8日
	*/
	@Transactional
	public void resetRoleVsMenu(Long roleId, Collection<Long> menuIds) {
		
		//先删除 , 后新增
		roleVsMenuMapper.delete(new LambdaQueryWrapper<RoleVsMenuEntity>().eq(RoleVsMenuEntity::getSysRoleId, roleId));
		
		for(Long menuId : menuIds) {
			
			RoleVsMenuEntity entity = new RoleVsMenuEntity();
			
			entity.setMenuId(menuId);
			entity.setSysRoleId(roleId);
			
			roleVsMenuMapper.insert(entity);
			
		}
		
	}
	
	
	
}
